﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WFONE
{
    
    public partial class Single01 : Form
    {
        public static CDeDataSl cdedata = new CDeDataSl() ;

        public Single01()
        {
            InitializeComponent();
            Visible = false;
            cdedata = TopWindows.cdevice.cDeDataSingleListHead;

            while (cdedata != null)
            {

                dateNameMe.Items.Add(cdedata.datetime.ToString());
                cdedata = cdedata.next;
            }
          

        }
      
        private void Form1_Load(object sender, EventArgs e)
        {

        }                
        public bool inputBool()//输入数据完整提示
        {
            if (tBGs.Text == "" || tBX0.Text == "" || tBX1.Text == ""|| tBX2.Text == ""|| tBX3.Text == "")                
            {
                return false;
            }
            else
            {
                return true;
            }         
 
        }
        private bool inputData()//输入数据
        {
            if (inputBool() == true)
            {
                CList.cInputListSl.dGs = Math.Round(double.Parse(tBGs.Text),3);
                CList.cInputListSl.dX0 = Math.Round(double.Parse(tBX0.Text),3);
                CList.cInputListSl.dX1 = Math.Round(double.Parse(tBX1.Text),3);
                CList.cInputListSl.dX2 = Math.Round(double.Parse(tBX2.Text),3);
                CList.cInputListSl.dX3 = Math.Round(double.Parse(tBX3.Text),3);
                cdedata.dRs = double.Parse(tBRS.Text);
                 cdedata.dRx = double.Parse(tBRX.Text);
                cdedata.dRp = double.Parse(tBRP.Text);
                cdedata.dGp = double.Parse(tBGP.Text);
                cdedata.dPAngle = double.Parse(tBAngle.Text);
               cdedata.dA1 = double.Parse(tBA1.Text);
               cdedata.dA2 = double.Parse(tBA2.Text);
                return true;
               
            }
            else
            {
                MessageBox.Show("请输入完整数据", "提示信息", MessageBoxButtons.OK);
                return false;
            }
        }
        
        private void RshowInput()//显示选定的记录时间对应的设备数据
        {
            //cdedata.datetime = dateTime;
            if(cdedata != null)
            {
                tBRS.Text = cdedata.dRs.ToString();
                tBRX.Text = cdedata.dRx.ToString();
                tBRP.Text = cdedata.dRp.ToString();
                tBGP.Text = cdedata.dGp.ToString();
                tBAngle.Text = cdedata.dPAngle.ToString();
                tBA1.Text = cdedata.dA1.ToString();
                tBA2.Text = cdedata.dA2.ToString();
            }
        }
        private void ResultShow()//显示计算结果
        {
            COutputListSl output = CList.cOutputListSl;
            tBU0.Text = output.dU0.ToString();
            tBAngleOne.Text = output.dAngleOne.ToString();
            tBGx.Text = output.dGX.ToString();
            if (output.dB1 == DMAX && output.dB2 == DMAX)
            {
                tBB1.Text = "ERROR";
                tBB2.Text = "ERROR";
                OutOfBalance();
            }
            else
            {
                tBB1.Text = output.dB1.ToString();
                tBB2.Text = output.dB2.ToString();
            }
           
        }
        
        private void Sbtn_Click(object sender, EventArgs e)
        {
            //cdevice = CDeviceList.SearchDevice(DeviceName.Text);
            if (inputData()) ;//传入数据
            { 
            cSingleAlg();
            ResultShow();
             }
        }
        
        public const double DMAX = 99999.9999;//平衡不能实现时B1和B2的特殊输出值，用于检测
        public void cSingleAlg()//根据输入，得出单面测量的计算结果
        {
            CInputListSl cintput = CList.cInputListSl;
            COutputListSl coutput = CList.cOutputListSl;
   
            double K = 0;
            K = (Math.Sqrt(cintput.dX1) + Math.Sqrt(cintput.dX2) + Math.Sqrt(cintput.dX3)) / (3 * Math.Sqrt(cintput.dX0));
            //求U0
           coutput.dU0 =Math.Round( (cintput.dGs - cdedata.dRs) / Math.Sqrt(K - 1),3);
            //求Gx
            coutput.dGX = Math.Round(coutput.dU0 / cdedata.dRx,3);
            //求角度
            double temp = 2 * Math.Sqrt(cintput.dX1) - Math.Sqrt(cintput.dX2) - Math.Sqrt(cintput.dX3);
            coutput.dAngleOne = Math.Atan(Math.Pow(3,0.5)*(Math.Sqrt(cintput.dX2)-Math.Sqrt(cintput.dX3))/
                temp);
            if (temp <= 0)
                coutput.dAngleOne =+ 180;
            //求B1和B2
            double x, y ,M;
            double b1, b2;
            x = Math.Cos(cdedata.dA1) + Math.Cos(cdedata.dA2) - (coutput.dU0 / (cdedata.dGp * cdedata.dRp)) * Math.Cos(coutput.dAngleOne);
            y = Math.Sin(cdedata.dA1) + Math.Sin(cdedata.dA2) - (coutput.dU0 / (cdedata.dGp * cdedata.dRp)) * Math.Cos(coutput.dAngleOne);
            M = Math.Sqrt(x)+Math.Sqrt(y);
            b1 = Math.Atan(y / x);
            if (x >= 0)           
                b1 = + 180;
            //平衡能力不足或B1和B2的值相差太小，设备无法实现时，将B1和B2的值设置为特殊值
            //供输出结果的方法检测
            if (M > 4)
            {
                //发现平衡能力不够时，直接将B1和B2设置为超大值
                //显示计算结果时，检测B1和B2的值是否为DMAX
                coutput.dB1 = DMAX;
                coutput.dB2 = DMAX;
            }
            else
            {
                b2 = Math.Cos(Math.Pow(M, 0.5) / 2);
                coutput.dB1 = Math.Round(b1 - b2,3);
                coutput.dB2 = Math.Round(b1 + b2,3);
                if (Math.Abs(coutput.dB1 - coutput.dB2) <= coutput.dAngleOne)
                {
                    coutput.dB1 = DMAX;
                    coutput.dB2 = DMAX;
                }
            }

        }
        public void OutOfBalance()
        {
            
        }

        private void dateNameMe_SelectedIndexChanged(object sender, EventArgs e)//改变DataTimeMe控件的值，判断选定查询的设备记录的时间
        {
            /*
             这部分有bug，未修改
            DateTime dateTime = new DateTime();
            if (dateNameMe.Text != "")
            {
                dateTime = Convert.ToDateTime(dateNameMe.Text);
            }
            else
            {
                dateTime = DateTime.Now;
            }
            if (TopWindows.cdevice != null)
            {
                cdedata = TopWindows.cdevice.SearchSl(dateTime);
            }
            RshowInput();
            */
           DateTime dateTime = new DateTime();
            try
            {
                if (dateNameMe.Text != "")
                {
                    dateTime = Convert.ToDateTime(dateNameMe.Text);
                    cdedata = TopWindows.cdevice.SearchSl(dateTime);
                    if(cdedata != null)
                    {
                        RshowInput();
                    }
                    else
                    {
                        MessageBox.Show("查询出错!请联系管理员", "提示", MessageBoxButtons.OK);
                    }
                }
                                
            }
            catch(Exception es)
            {
                MessageBox.Show(es.ToString());
            }

        
        }

        private void Qbtn_Click(object sender, EventArgs e)
        {
            COutputListSl coutput = CList.cOutputListSl;
            if (!(coutput.dB1==DMAX||coutput.dB2==DMAX))
            {
                cdedata.dA1 = coutput.dB1;
                cdedata.dA2 = coutput.dB2;
                tBA1.Text = cdedata.dA1.ToString();
                tBA2.Text = cdedata.dA2.ToString();
            }
        }
    }

    
}
